// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/04/Fill.asm

// Runs an infinite loop that listens to the keyboard input.
// When a key is pressed (any key), the program blackens the screen,
// i.e. writes "black" in every pixel;
// the screen should remain fully black as long as the key is pressed. 
// When no key is pressed, the program clears the screen, i.e. writes
// "white" in every pixel;
// the screen should remain fully clear as long as no key is pressed.

// Put your code here.

@SCREEN
D=A

// addr = SCREEN
@addr
M=D

(WRITE)
	@KBD
	D=M
	@CLEAR
	D;JEQ

	// if (*addr == 24575) break; // max screen
	@addr
	D=M
	@24575
	D=D-A
	@CLEAR
	D;JGE

	@addr
	A=M
	M=-1
	@addr
	M=M+1

	@WRITE
	0;JMP

(CLEAR)
	@KBD
	D=M
	@WRITE
	D;JNE

	// if (*addr == SCREEN) break
	@addr
	D=M
	@SCREEN
	D=D-A
	@WRITE
	D;JLE

	@addr
	A=M
	M=0
	@addr
	M=M-1

	@CLEAR
	0;JMP
